package umls.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class SemGroupConfReader {

	private TreeMap<String, SemanticType> AllowedSemanticTypes;
	private HashSet<String> AllowedSemanticGroups;

	private String confFilePath = "";

	public SemGroupConfReader(String path) {
		this.confFilePath = path;
		this.AllowedSemanticGroups = new HashSet<String>();
		// the tui is the key
		this.AllowedSemanticTypes = new TreeMap<String, SemanticType>();
	}

	public void ReadAllowedSemanticGroups() {
		File confsemFile = new File(confFilePath);
		try {
			BufferedReader input = new BufferedReader(new FileReader(
					confsemFile));

			String line = "";

			while ((line = input.readLine()) != null) {
				this.AllowedSemanticGroups.add(line);
			}
		} catch (IOException ioe) {
			// TODO: handle exception
			ioe.printStackTrace();
		}
	}

	public void ReadAllowedSemanticTypes() {
		File semgroupFile = new File("MedServiceData/SemGroups.txt");

		try {
			BufferedReader input = new BufferedReader(new FileReader(
					semgroupFile));

			String line = "";

			while ((line = input.readLine()) != null) {
				String[] semgroupsplittedline = line.split("\\|");

				String group_id = semgroupsplittedline[0];
				String group_name = semgroupsplittedline[1];
				String tui = semgroupsplittedline[2];
				String sty = semgroupsplittedline[3];

				if (AllowedSemanticGroups.contains(group_name)) {
					AllowedSemanticTypes.put(tui, new SemanticType(tui, sty));
				}
			}
		} catch (IOException ioe) {
			// TODO: handle exception
			ioe.printStackTrace();
		}
	}

	public void PrintAllowedSemanticTypes() {
		Set semtype_set = AllowedSemanticTypes.entrySet();

		Iterator semtype_it = semtype_set.iterator();

		while (semtype_it.hasNext()) {
			Map.Entry<String, SemanticType> allowedsemtype = (Map.Entry<String, SemanticType>) semtype_it
					.next();

			SemanticType semtype = allowedsemtype.getValue();

			System.out.println(semtype.GetTUI() + " " + semtype.GetSTY());
		}
	}

	public boolean isAllowedSemanticType(String tui) {
		if (AllowedSemanticTypes.containsKey(tui)) {
			return true;
		} else {
			return false;
		}
	}
}